home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap10 / PopMenu / PopMenu.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.6 KB  |  137 lines

  1. /*----------------------------------------
  2.    POPMENU.C -- Popup Menu Demonstration
  3.                 (c) Charles Petzold, 1998
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "resource.h"
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. HINSTANCE hInst ;
  12. TCHAR     szAppName[] = TEXT ("PopMenu") ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16. {
  17.      HWND     hwnd ;
  18.      MSG      msg ;
  19.      WNDCLASS wndclass ;
  20.      
  21.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.      wndclass.lpfnWndProc   = WndProc ;
  23.      wndclass.cbClsExtra    = 0 ;
  24.      wndclass.cbWndExtra    = 0 ;
  25.      wndclass.hInstance     = hInstance ;
  26.      wndclass.hIcon         = LoadIcon (NULL, szAppName) ;
  27.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  29.      wndclass.lpszMenuName  = NULL ;
  30.      wndclass.lpszClassName = szAppName ;
  31.      
  32.      if (!RegisterClass (&wndclass))
  33.      {
  34.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  35.                       szAppName, MB_ICONERROR) ;
  36.           return 0 ;
  37.      }
  38.      
  39.      hInst = hInstance ;
  40.      
  41.      hwnd = CreateWindow (szAppName, TEXT ("Popup Menu Demonstration"),
  42.                           WS_OVERLAPPEDWINDOW,
  43.                           CW_USEDEFAULT, CW_USEDEFAULT,
  44.                           CW_USEDEFAULT, CW_USEDEFAULT,
  45.                           NULL, NULL, hInstance, NULL) ;
  46.      
  47.      ShowWindow (hwnd, iCmdShow) ;
  48.      UpdateWindow (hwnd) ;
  49.      
  50.      while (GetMessage (&msg, NULL, 0, 0))
  51.      {
  52.           TranslateMessage (&msg) ;
  53.           DispatchMessage (&msg) ;
  54.      }
  55.      return msg.wParam ;
  56. }
  57.  
  58. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  59. {
  60.      static HMENU hMenu ;
  61.      static int   idColor [5] = { WHITE_BRUSH,  LTGRAY_BRUSH, GRAY_BRUSH,
  62.                                   DKGRAY_BRUSH, BLACK_BRUSH } ;
  63.      static int   iSelection = IDM_BKGND_WHITE ;
  64.      POINT        point ;
  65.      
  66.      switch (message)
  67.      {
  68.      case WM_CREATE:
  69.           hMenu = LoadMenu (hInst, szAppName) ;
  70.           hMenu = GetSubMenu (hMenu, 0) ;
  71.           return 0 ;
  72.  
  73.      case WM_RBUTTONUP:
  74.           point.x = LOWORD (lParam) ;
  75.           point.y = HIWORD (lParam) ;
  76.           ClientToScreen (hwnd, &point) ;
  77.           
  78.           TrackPopupMenu (hMenu, TPM_RIGHTBUTTON, point.x, point.y, 
  79.                           0, hwnd, NULL) ;
  80.           return 0 ;
  81.           
  82.      case WM_COMMAND:
  83.           switch (LOWORD (wParam))
  84.           {
  85.           case IDM_FILE_NEW:
  86.           case IDM_FILE_OPEN:
  87.           case IDM_FILE_SAVE:
  88.           case IDM_FILE_SAVE_AS:
  89.           case IDM_EDIT_UNDO:
  90.           case IDM_EDIT_CUT:
  91.           case IDM_EDIT_COPY:
  92.           case IDM_EDIT_PASTE:
  93.           case IDM_EDIT_CLEAR:
  94.                MessageBeep (0) ;
  95.                return 0 ;
  96.                
  97.           case IDM_BKGND_WHITE:         // Note: Logic below
  98.           case IDM_BKGND_LTGRAY:        //   assumes that IDM_WHITE
  99.           case IDM_BKGND_GRAY:          //   through IDM_BLACK are
  100.           case IDM_BKGND_DKGRAY:        //   consecutive numbers in
  101.           case IDM_BKGND_BLACK:         //   the order shown here.
  102.                
  103.                CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;
  104.                iSelection = LOWORD (wParam) ;
  105.                CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;
  106.                
  107.                SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG) 
  108.                     GetStockObject 
  109.                          (idColor [LOWORD (wParam) - IDM_BKGND_WHITE])) ;
  110.                
  111.                InvalidateRect (hwnd, NULL, TRUE) ;
  112.                return 0 ;
  113.                
  114.           case IDM_APP_ABOUT:
  115.                MessageBox (hwnd, TEXT ("Popup Menu Demonstration Program\n")
  116.                                  TEXT ("(c) Charles Petzold, 1998"),
  117.                            szAppName, MB_ICONINFORMATION | MB_OK) ;
  118.                return 0 ;
  119.                
  120.           case IDM_APP_EXIT:
  121.                SendMessage (hwnd, WM_CLOSE, 0, 0) ;
  122.                return 0 ;
  123.                
  124.           case IDM_APP_HELP:
  125.                MessageBox (hwnd, TEXT ("Help not yet implemented!"),
  126.                            szAppName, MB_ICONEXCLAMATION | MB_OK) ;
  127.                return 0 ;
  128.           }
  129.           break ;
  130.           
  131.      case WM_DESTROY:
  132.           PostQuitMessage (0) ;
  133.           return 0 ;
  134.      }
  135.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  136. }
  137.